﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MemberMgr.Data.BusinessObjects;
using MemberMgr.Data.Base;
using Ext.Net;
using MemberMgr.Data.ManagerObjects;
using System.Data;

namespace MemberMgr.Website.Admin
{
    public partial class ProductTypeMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest && !this.IsPostBack)
            {

            }
        }

        [DirectMethod]
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strid = this.txtID.Text.Trim();
            string name = this.txtTypeName.Text.Trim();
            string type = this.txtKey.Text.Trim();
            string comment = this.txtComment.Text.Trim();

            ItSystemManager sysMgr = Common.CommonUtil.managerFactory.GettSystemManager();

            sysMgr.Session.BeginTransaction();

            try
            {
                if (string.IsNullOrEmpty(strid))
                {
                    //Add

                    tSystem system = new tSystem();

                    system.Datakbn = Common.CommonConst.KBN_PRODUCTTYPE;
                    system.Datakey = type;
                    system.Data1 = name;
                    system.Data10 = comment;

                    sysMgr.Save(system);
                }
                else
                {
                    //Update

                    tSystem system = sysMgr.GetById(strid);

                    if (system != null)
                    {
                        system.Datakey = type;
                        system.Data1 = name;
                        system.Data10 = comment;

                        sysMgr.Update(system);
                    }

                }

                sysMgr.Session.CommitTransaction();
            }
            catch (Exception ex)
            {
                Common.CommonUtil.logger.Error(ex.Message, ex);
                sysMgr.Session.RollbackTransaction();
            }

            this.winDetail.Hide();

            BindDataSource();

        }

        [DirectMethod]
        protected void storeProduct_RefreshData(object sender, EventArgs e)
        {

        }

        [DirectMethod]
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataSource();
        }

        [DirectMethod]
        public void editProduct(string productID)
        {
            ItSystemManager sysMgr = Common.CommonUtil.managerFactory.GettSystemManager();

            tSystem system = sysMgr.GetById(productID);

            if (system != null)
            {
                this.panelDetail.SetValues(new
                {
                    system.Id,
                    system.Datakey,
                    system.Data1,
                    system.Data10
                });

                this.winDetail.Show();
            }
        }

        private void BindDataSource()
        {
            string name = this.txtSearchName.Text;

            ItSystemManager sysMgr = Common.CommonUtil.managerFactory.GettSystemManager();
            IList<tSystem> list = null;

            if (!string.IsNullOrEmpty(name))
            {
                list = sysMgr.GetSystemSettingsByNameAndKBN(name,Common.CommonConst.KBN_PRODUCTTYPE);
            }
            else
            {
                list = sysMgr.GetSystemSettingsByKBN(Common.CommonConst.KBN_PRODUCTTYPE);
            }

            if (list != null)
            {
                DataTable dt = this.GetMemberInfoDataTable();

                foreach (tSystem item in list)
                {
                    dt.Rows.Add(
                        item.Id,
                        item.Datakbn,
                        item.Datakey,
                        item.Data1,
                        item.Data10
                        );
                }

                this.storeProduct.DataSource = dt;
                this.storeProduct.DataBind();
            }
        }

        private DataTable GetMemberInfoDataTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[] { 
                new DataColumn() { ColumnName="ID",DataType=typeof(string)},
                new DataColumn() { ColumnName="DATAKBN",DataType=typeof(string)},
                new DataColumn() { ColumnName="DATAKEY",DataType=typeof(string)},
                new DataColumn() { ColumnName="DATA1",DataType=typeof(string)},
                new DataColumn() { ColumnName="DATA10",DataType=typeof(string)}
            });

            return dt;
        }
    }
}
